import {
  registerMicroApps,
  prefetchApps,
  addGlobalUncaughtErrorHandler,
  removeGlobalUncaughtErrorHandler,
  start,
} from "qiankun";
// 子应用注册信息
import apps from "./apps";

prefetchApps(apps);

const errorHandler = (event: Event | string) => {
  const { message: msg } = event as any;
  // 加载失败时提示
  if (msg && msg.includes("died in status LOADING_SOURCE_CODE")) {
    console.error("子应用加载失败，请检查应用是否可运行");
  }
};

/**
 * 添加全局的未捕获异常处理器
 */
export const addGlobalErrorHandler = () => {
  addGlobalUncaughtErrorHandler(errorHandler);
};

/**
 * 移除全局的未捕获异常处理器
 */
export const removeGlobalErrorHandler = () => {
  removeGlobalUncaughtErrorHandler(errorHandler);
};

// 导出 qiankun 的启动函数
export const startQiankun = (config: any = {}) => {
  addGlobalErrorHandler();

  /**
   * 注册子应用
   * 第一个参数 - 子应用的注册信息
   * 第二个参数 - 全局生命周期钩子
   */
  registerMicroApps(apps, {
    // qiankun 生命周期钩子 - 加载前
    beforeLoad: (app: any) => {
      // 加载子应用前，加载进度条
      console.log("before load", app.name);
      return Promise.resolve();
    },
    // qiankun 生命周期钩子 - 挂载后
    afterMount: (app: any) => {
      // 加载子应用前，进度条加载完成
      console.log("after mount", app.name);
      return Promise.resolve();
    },
    beforeUnmount: (app: any): any => {
      console.log("before unmount", app.name);
    },
  });

  start(config);
};
